@extends('layouts/index')

@section('title', $default_name)

@section('content')
    <div class="layui-card">
        <div class="layui-card-header notselect">
            <span class="layui-icon layui-icon-next font-s10 color-desc margin-right-5"></span> {{ $default_name }}
            <div class="pull-right">
                @if(array_key_exists('base.menu.add',$btns))
                    <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="addmenu()">
                        {{ $btns['base.menu.add'] }}
                    </button>
                @endif
            </div>
        </div>
        <div class="layui-card-body">
            <table class="layui-table">
                <thead>
                <tr>
                    <th class="text-center nowrap" width="2%">ID</th>
                    <th class="text-center nowrap" width="2%">{{ trans('common.sort') }}</th>
                    <th class="text-left nowrap" width="5%">{{ trans('common.type') }}</th>
                    <th class="text-left nowrap" width="30%">{{ trans('menu.menu_name') }}</th>
                    <th class="text-left nowrap" width="20%">{{ trans('menu.menu_rule') }}</th>
                    <th class="text-left nowrap" width="5%">{{ trans('menu.icon') }}</th>
                    <th class="text-left nowrap" width="8%">{{ trans('common.status') }}</th>
                    <th class="text-left nowrap" width="30%">{{ trans('common.operation') }}</th>
                </tr>
                </thead>
                <tbody>

                @foreach($menus as $menu)
                    <tr data-dbclick="">
                        <td class="text-left nowrap">
                            {{ $menu->id }}
                        </td>
                        <td class="text-left nowrap list-table-sort-td">
                            <input type="number" class="list-sort-input" value="{{ $menu->sort }}">
                        </td>
                        <td class="text-left nowrap">
                            {{ $types[$menu->type] }}
                        </td>
                        <td class="text-left nowrap">
                            {!! $menu->name !!}
                        </td>
                        <td class="text-left nowrap">
                            {{ $menu->rule }}
                        </td>
                        <td class="text-left nowrap">
                            <i class="{{ $menu->icon }}"></i>
                        </td>
                        <td class="text-left nowrap">
                            @if($menu->status == \App\Models\AdminMenusModel::MENUSHOW)
                                <span class="layui-badge layui-bg-green">{{ trans('common.show') }}</span>
                            @elseif($menu->status==\App\Models\AdminMenusModel::MENUHIDE)
                                <span class="layui-badge">{{ trans('common.hide') }}</span>
                            @endif
                        </td>
                        <td class="text-left nowrap">
                            @if(array_key_exists('base.menu.alone.add',$btns))
                                <button
                                    {{ isset($menu) && $menu->type ==\App\Models\AdminMenusModel::BUTTON ? "disabled":""  }}
                                    class="layui-btn layui-btn-xs {{ isset($menu) && $menu->type ==\App\Models\AdminMenusModel::BUTTON ? "layui-btn-disabled":""  }}"
                                    onclick="addAloneMenu({{ $menu->id }},{{ $menu->pid }})">{{ $btns['base.menu.alone.add'] }}
                                </button>
                            @endif
                            @if(array_key_exists('base.menu.modify',$btns))
                                <button class="layui-btn layui-btn-normal layui-btn-xs"
                                        onclick="modifyMenu({{ $menu->id }})">{{ $btns['base.menu.modify'] }}
                                </button>
                            @endif
                            @if(array_key_exists('base.menu.del',$btns))
                                <button class="layui-btn layui-btn-danger layui-btn-xs"
                                        onclick="deleteMenu({{ $menu->id }})">{{ $btns['base.menu.del'] }}
                                </button>
                            @endif
                            @if(array_key_exists('base.menu.hide',$btns) && $menu->status == \App\Models\AdminMenusModel::MENUSHOW)
                                <button class="layui-btn layui-btn-warm layui-btn-xs"
                                        onclick="disableMenu({{ $menu->id }},{{ $menu->status }})">{{ $btns['base.menu.hide'] }}
                                </button>
                            @endif
                            @if(array_key_exists('base.menu.show',$btns) && $menu->status == \App\Models\AdminMenusModel::MENUHIDE)
                                <button class="layui-btn layui-btn-xs"
                                        onclick="disableMenu({{ $menu->id }},{{ $menu->status }})">{{ $btns['base.menu.show'] }}
                                </button>
                            @endif
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>

    <script type="text/javascript">
        function addmenu() {
            layer.open({
                type: 2,
                title: '{{ trans('menu.add_menu') }}',
                area: ['600px', '500px'],
                content: '{{ url('menu/show') }}'
            });
        }

        function modifyMenu(id) {
            layer.open({
                type: 2,
                title: '{{ trans('menu.modify_menu') }}',
                area: ['600px', '500px'],
                content: "{{ url('menu/show') }}?id=" + id
            });
        }

        function addAloneMenu(id, pid) {
            layer.open({
                type: 2,
                title: "{{ trans('menu.add_menu') }}",
                area: ['600px', '500px'],
                content: "{{ url('menu/show') }}?id=" + id + "&pid=" + pid
            });
        }

        function disableMenu(id, status) {
            var index = layer.confirm("{{ trans('menu.confirm_operation_menu') }}", {
                title: "{{ trans('common.system_info') }}",
                icon: 3,
                anim: 0,
                btn: ["{{ trans('common.sure') }}", "{{ trans('common.cancel') }}"],
            }, function () {
                $.ajax({
                    url: '{{ url('menu/disable') }}',
                    type: 'POST',
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    ContentType: "application/json; charset=utf-8",
                    data: {"id": id, "status": status},
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg("{{ trans('common.operation_success') }}", {
                                time: 1000
                            }, function () {
                                window.location.reload();
                            })
                        } else {
                            layer.msg(result.msg)
                        }
                    }
                });
            }, function () {
                layer.close(index)
            });
        }


        function deleteMenu(id) {
            var index = layer.confirm("{{ trans('menu.confirm_del_menu') }}", {
                title: "{{ trans('common.system_info') }}",
                icon: 3,
                anim: 0,
                btn: ["{{ trans('common.sure') }}", "{{ trans('common.cancel') }}"],
            }, function () {
                $.ajax({
                    url: '{{ url('menu/del') }}',
                    type: 'POST',
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    ContentType: "application/json; charset=utf-8",
                    data: {"id": id},
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg("{{ trans('common.operation_success') }}", {
                                time: 1000
                            }, function () {
                                window.location.reload();
                            })
                        } else {
                            layer.msg(result.msg)
                        }
                    }
                });
            }, function () {
                layer.close(index)
            });
        }
    </script>
@endsection
